/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package questionbook.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import questionbook.Configuration.QuestionbookConfiguration;
import questionbook.exception.IndividualAlreadyExistsException;
import questionbook.model.Category;
import questionbook.service.common.GenericServiceImpl;
import questionbook.standard.Qbook;

/**
 *
 * @author stefanstift
 */
public class CategoryServiceBean extends GenericServiceImpl implements CategoryService {

    public void persist(Category c) {

       this.storeManager.getStore().add(c.getUri(), RDF.TYPE, Qbook.getInstance().getCategoryURI());

       if(c.getName() != null && !c.getName().isEmpty())
           this.storeManager.getStore().add(c.getUri(), RDFS.LABEL, c.getNameLiteral());
        
    }

    public Category get(URI uri)
    {
        Category c = new Category(uri);

        List<Statement> n = this.storeManager.getStore().tuplePattern(uri, RDFS.LABEL, null);

        if(n != null && n.size() > 0)
            c.setName(n.get(0).getObject().stringValue());

        return c;
    }

    public Category getIdentifiedCategory() throws IndividualAlreadyExistsException{

        int i = 0;
        String categoryBase = "/qbCategory";
        Date d = new Date();
        String timestamp = Long.toString(d.getTime());

        String id = QuestionbookConfiguration.getInstance().getBaseURL() + categoryBase + timestamp;
        URI uri = this.storeManager.getStore().getURIref(id);

        List check = this.storeManager.getStore().tuplePattern(uri, null, null);

        while(check != null || !check.isEmpty())
        {
            if(i == 10)
            {
                break;
            }

            d = new Date();
            timestamp = Long.toString(d.getTime());
            id = QuestionbookConfiguration.getInstance().getBaseURL() + categoryBase + timestamp;
            uri = this.storeManager.getStore().getURIref(id);

            check = this.storeManager.getStore().tuplePattern(uri, null, null);

            i++;
        }

        if(check == null || check.isEmpty())
        {
            Category c = new Category(uri);
            return c;
        }

        throw new IndividualAlreadyExistsException("Tried to create Answer with URI " + id + " - but it already exists in the Store");
    }
    
     public List<Category> getAll() {

        List<Category> resultList = new ArrayList<Category>();

        List<Map> res = this.storeManager.getStore().runSPARQL("SELECT ?category ?name"
                + " WHERE { ?category <" + RDF.TYPE.stringValue() + "> <" + Qbook.getInstance().getCategoryURI() +  "> ."
                + " ?category <" + RDFS.LABEL.stringValue() + "> ?name ."
                + "}");

        for(Map m : res)
        {
            Category c = new Category(((Value) m.get("category")).stringValue());
            c.setName(((Value) m.get("name")).stringValue());

            resultList.add(c);
        }

        return resultList;
    }




}
